home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / pdf / c++ / Save < prev    next >
Text File  |  2003-02-14  |  9KB  |  292 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #include "Save.h"
  39. #include "DocView.h"
  40. #include "fstream.h"
  41. #include "strstream.h"
  42. #include "Document.h"
  43. #include "file.h"
  44. #include "DrawOutputDevice.h"
  45. #include "GuiHourGlass.h"
  46. #include "TextOutputdev.h"
  47. #include "PSOutputDev.h"
  48. #include "Page.h"
  49. #include "guilib:gfx.h"
  50.  
  51. Save::Save(DocView& v, GuiWindow& ancestor)
  52.   : saveAs("SaveAs"),
  53.     view(v),
  54.     aboutToBeShownTarget(&ancestor,GuiSaveAs::AboutToBeShown::Event,this,Save::aboutToBeShown),
  55.     saveToFileTarget(&ancestor,GuiSaveAs::SaveToFile::Event,this,Save::saveAsDrawfile)
  56. {
  57. }
  58.  
  59. //*************************************************************************
  60.  
  61. Save::~Save() {}
  62.  
  63. //*************************************************************************
  64.  
  65. Claim Save::aboutToBeShown(GuiToolboxEvent&, const GuiIdBlock& id_block)
  66. {
  67.   switch (id_block.parent.component)
  68.   {
  69.     case 0: //pdf
  70.             saveAs.setFileType(0xadf);
  71.             saveToFileTarget.setMethod(this,Save::saveAsPdf);
  72.             break;
  73.     case 2: //postscript
  74.             saveAs.setFileType(0xff5);
  75.             saveToFileTarget.setMethod(this,Save::saveAsPostscript);
  76.             break;
  77.     case 3: //text;
  78.             saveAs.setFileType(0xfff);
  79.             saveToFileTarget.setMethod(this,Save::saveAsText);
  80.             break;
  81.     case 4://whole doc as text
  82.             saveAs.setFileType(0xfff);
  83.             saveToFileTarget.setMethod(this,Save::saveAllAsText);
  84.             break;
  85.     case 5://whole doc as drawfile
  86.             saveAs.setFileType(0x1000);
  87.             saveToFileTarget.setMethod(this,Save::saveAllAsDrawfiles);
  88.             break;
  89.     case 6://page as postscript
  90.             saveAs.setFileType(0xff5);
  91.             saveToFileTarget.setMethod(this,Save::savePageAsPostscript);
  92.             break;
  93.     default: //drawfile
  94.             saveAs.setFileType(0xaff);
  95.             saveToFileTarget.setMethod(this,Save::saveAsDrawfile);
  96.   }
  97.   return CLAIM;
  98. }
  99.  
  100. //*************************************************************************
  101.  
  102. Claim Save::saveAsPdf(GuiToolboxEvent& ev,const GuiIdBlock&)
  103. {
  104.   GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
  105.   ifstream in(view.getDocument().getFileName().c_str());
  106.   ofstream out(file.filename);
  107.   out << in.rdbuf();
  108.   if ((in.good() && out.good())) setFileType(file.filename,0xadf);
  109.   saveAs.fileSaveCompleted((in.good() && out.good()?1:0),file.filename);
  110.   return CLAIM;
  111. }
  112.  
  113. //*************************************************************************
  114.  
  115. Claim Save::saveAsDrawfile(GuiToolboxEvent& ev,const GuiIdBlock&)
  116. {
  117.   GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
  118.   int ok= (view.getOutDev().getDrawFile().save(file.filename)==0?1:0);
  119.   saveAs.fileSaveCompleted(ok,file.filename);
  120.   return CLAIM;
  121. }
  122.  
  123. //*************************************************************************
  124.  
  125.  
  126. Claim Save::saveAsPostscript(GuiToolboxEvent& ev,const GuiIdBlock&)
  127. {
  128.   GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
  129.  
  130.   GuiHourglass hourglass;
  131.   PSOutputDev out(
  132.                    file.filename,
  133.                    view.getDocument().getPDFDoc().getXRef(),
  134.                    view.getDocument().getCatalog(),
  135.                    1,
  136.                    view.getDocument().getPageCount(),
  137.                    psModePS
  138.                  );
  139.  
  140.   bool ok=out.isOk();
  141.   if (ok)
  142.   {
  143.     int i;
  144.     int pages=view.getDocument().getPageCount();
  145.     for (i=1;i<=pages;i++)
  146.     {
  147.       GuiHourglass::percentage(i*100/pages);
  148.       view.getDocument().getPage(i,&out,72,0);
  149.       if (gfx::inkey(-113)==-1) break;
  150.     }
  151.   }
  152.   if (ok) setFileType(file.filename,0xff5);
  153.   saveAs.fileSaveCompleted(ok?1:0,file.filename);
  154.   return CLAIM;
  155. }
  156.  
  157. //*************************************************************************
  158.  
  159.  
  160. Claim Save::savePageAsPostscript(GuiToolboxEvent& ev,const GuiIdBlock&)
  161. {
  162.   GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
  163.  
  164.   GuiHourglass hourglass;
  165.   PSOutputDev out(
  166.                    file.filename,
  167.                    view.getDocument().getPDFDoc().getXRef(),
  168.                    view.getDocument().getCatalog(),
  169.                    view.getPage(),
  170.                    view.getPage(),
  171.                    psModePS
  172.                  );
  173.  
  174.   bool ok=out.isOk();
  175.   if (ok)
  176.   {
  177.     view.getDocument().getPage(view.getPage(),&out,72,0);
  178.     setFileType(file.filename,0xff5);
  179.   }
  180.   saveAs.fileSaveCompleted(ok?1:0,file.filename);
  181.   return CLAIM;
  182. }
  183.  
  184. //*************************************************************************
  185.  
  186.  
  187. Claim Save::saveAsText(GuiToolboxEvent& ev,const GuiIdBlock&)
  188. {
  189.   GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
  190.   TextOutputDev out(file.filename,0,0,0);
  191.   bool ok=out.isOk();
  192.   GuiHourglass hourglass;
  193.   if (ok)
  194.   {
  195.     view.getDocument().getPage(view.getPage(),&out,72,0);
  196.     setFileType(file.filename,0xfff);
  197.   }
  198.   if (ok) 
  199.   saveAs.fileSaveCompleted((ok?1:0),file.filename);
  200.   return CLAIM;
  201. }
  202.  
  203. //*************************************************************************
  204.  
  205. Claim Save::saveAllAsText(GuiToolboxEvent& ev,const GuiIdBlock&)
  206. {
  207.   GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
  208.  
  209.   TextOutputDev out(file.filename,0,0,0);
  210.   bool ok=out.isOk();
  211.   GuiHourglass hourglass;
  212.   if (ok)
  213.   {
  214.     int i;
  215.     int pages=view.getDocument().getPageCount();
  216.     for (i=1;i<=pages;i++)
  217.     {
  218.       GuiHourglass::percentage(i*100/pages);
  219.       view.getDocument().getPage(i,&out,72,0);
  220.       if (gfx::inkey(-113)==-1) break;
  221.     }
  222.     setFileType(file.filename,0xfff);
  223.   }
  224.  
  225.   saveAs.fileSaveCompleted((ok?1:0),file.filename);
  226.   return CLAIM;
  227. }
  228.  
  229. //*************************************************************************
  230.  
  231. Claim Save::saveAllAsDrawfiles(GuiToolboxEvent& ev,const GuiIdBlock&)
  232. {
  233.   GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
  234.  
  235.   char buf[280];
  236.   ostrstream name(buf,sizeof(buf));
  237.  
  238.   bool ok =  (isDirectory(file.filename) || createDirectory(file.filename));
  239.  
  240.   DrawOutputDevice out;
  241.   GuiHourglass hourglass;
  242.   out.setMoreColours(gTrue);
  243.   out.setNoImages(view.getChoices().getNoImages());
  244.   out.setNoText(view.getChoices().getNoText());
  245.  
  246.   if (ok)
  247.   {
  248.     int  dir=0;
  249.     bool  pathOk; 
  250.     int i;
  251.     int pages=view.getDocument().getPageCount();
  252.     for (i=1;i<=pages;i++)
  253.     {    
  254.       pathOk=0; 
  255.       while (!pathOk)
  256.       {
  257.         name.seekp(0);
  258.         name << file.filename << ".Pages";
  259.         name.fill('0');
  260.         name.width(2);
  261.         name << dir << ".p";
  262.         name.fill('0');
  263.         name.width(4);
  264.         name << i;
  265.  
  266.         ofstream check_file(name.str());
  267.         if (!check_file)     
  268.         {
  269.           dir++;
  270.           name.seekp(0);
  271.           name << file.filename<< ".Pages";
  272.           name.fill('0');
  273.           name.width(2);
  274.           name << dir;
  275.           if (!(isDirectory(name.str()) || createDirectory(name.str()) )) break;
  276.         }
  277.         else pathOk=1;
  278.       }
  279.  
  280.       if (!pathOk) break;
  281.  
  282.       GuiHourglass::percentage(i*100/pages);
  283.       view.getDocument().getPage(i,&out,DrawOutputDevice::DPI,0);
  284.       if (out.getDrawFile().save(name.str())!=0) break;
  285.       if (gfx::inkey(-113)==-1) break;
  286.     }
  287.   }
  288.  
  289.   saveAs.fileSaveCompleted((ok?1:0),file.filename);
  290.   return CLAIM;
  291. }
  292.